import { defineStore } from "pinia";
import Cookies from "js-cookie";

const cookiesStorage: any = {
  setItem(key: string, state: any) {
    return Cookies.set(key, state.accessToken, { expires: 3 });
  },
  getItem(key: string) {
    return JSON.stringify({ accessToken: Cookies.get(key) });
  },
};
let storeHome = defineStore("home", {
  persist: {
    enabled: true,
    strategies: [
      {
        storage: sessionStorage,
        paths: ["mobile", "username", "Token"],
      },
      // {
      //   key: "practiveToken", //如果使用cookie存储，必须加上key，否则value值是undefined
      //   storage: cookiesStorage,
      //   paths: ["practiveToken"],
      // },
    ],
  },
  state: () => {
    return {
      mobile: "",
      username: "",
      Token: "",
    };
  },
  getters: {},
  actions: {
    setUserInfoMsg(data: any) {
      this.Token = data.token;
      this.username = data.username;
      this.mobile = data.mobile;
      return new Promise((res) => {
        res(true);
      });
    },
    clearToken() {
      this.Token = "";
      this.username = "";
      this.mobile = "";
    },
  },
});
export const getToken = () => {
  return storeHome().Token;
};
export default storeHome;
